package com.qf.web.controller;

import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.entity.SysMenuEntity;
import com.qf.api.form.MenuForm;
import com.qf.api.mybatis.page.BaseController;
import com.qf.api.service.SysMenuService;
import com.qf.api.vo.MenuVO;
import com.qf.api.vo.RouteVO;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author: sin
 * @Date: 2025/5/7 15:33
 * @Description:
 **/
@Slf4j
@RestController
@Tag(name = "02.菜单接口", description = "菜单相关接口（包括获取路由信息）")
@ApiOperationLogTitle("菜单模块")
@RequestMapping("/admin/menus")
public class MenuController extends BaseController {

    @Resource
    private SysMenuService menuService;

    @Operation(summary = "01.获取当前用户路由信息", description = "根据权限构建当前用户的路由菜单结构")
    @GetMapping("/routers")
    public ResponseResult<List<RouteVO>> getUserRouters() {
        List<RouteVO> routes = menuService.buildRouterTree();
        return ResponseResult.success(routes);
    }

    @Operation(summary = "02.获取菜单列表", description = "查询所有菜单")
    @GetMapping
    public ResponseResult<List<MenuVO>> getMenuList() {
        List<MenuVO> menus = menuService.getMenuList();
        return ResponseResult.success(menus);
    }

    @Operation(summary = "03.新增菜单", description = "新增一个菜单项（目录、菜单、按钮、外链、iframe）")
    @ApiOperationLog(desc = "新增菜单", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseResult<Boolean> addMenu(@RequestBody @Valid MenuForm menu) {
        SysMenuEntity sysMenu = new SysMenuEntity();
        BeanUtils.copyProperties(menu,sysMenu);
        return isOk(menuService.save(sysMenu));
    }

    @Operation(summary = "04.修改菜单", description = "根据菜单ID修改对应菜单信息")
    @ApiOperationLog(desc = "修改菜单", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping("/{id}")
    public ResponseResult<Boolean> updateById(@Parameter(description = "菜单ID", required = true) @PathVariable Long id,@RequestBody SysMenuEntity menu) {
        menu.setId(id);
        return isOk(menuService.updateById(menu));
    }

    @Operation(summary = "05.删除菜单", description = "根据菜单ID集合删除多个菜单，支持批量删除")
    @ApiOperationLog(desc = "删除菜单", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseResult<Boolean> removeById(@RequestBody List<Long> menuIds) {
        return isOk(menuService.removeBatchByIds(menuIds));
    }
}

